package cosplay.Controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import cosplay.Beans.Comments;
import cosplay.DAO.CommentDAOImpl;
import cosplay.DAO.UserDAOImpl;
import cosplay.Services.CacheServices;
import cosplay.Services.CommonServices;

@Controller
public class CommentsController extends MultiActionController{
	
	@Autowired
	CommentDAOImpl commentdao;
	@Autowired
	UserDAOImpl userdao;
	private Cache cache=CacheManager.create().getCache("COSPLAY_CACHE");
	
	@RequestMapping(value="/addcomment.htm",method=RequestMethod.GET)
	protected ModelAndView handelShowAdd(HttpServletRequest req, HttpServletResponse res) throws Exception
	{
		ModelMap model =new ModelMap();
		model.addAttribute("target","addcomment.htm");
		model.addAttribute("message","please enter infomation");
		return new ModelAndView("commentmodify","model",model);
	}
	
	@RequestMapping(value="/addcomment.htm",method=RequestMethod.POST)
	protected ModelAndView handelActAdd(HttpServletRequest req,HttpServletResponse res) throws Exception
	{
		Comments comment=getCommentFromRequest(req);
		int id=commentdao.add(comment);
		ModelMap model=new ModelMap();
		if (id>0)
		{
			res.setIntHeader("sucess", 1);
			model.addAttribute("target","editcomment.htm");
			model.addAttribute("message","Add successed");
		}
		else
		{
			res.setIntHeader("sucess", 0);
			model.addAttribute("target","addcomment.htm");
			model.addAttribute("message","Add failed");
		}
		model.addAttribute("comment",comment);
		return new ModelAndView("commentmodify","model",model);
	}
	
	@RequestMapping(value="/editcomment.htm",method=RequestMethod.GET)
	protected ModelAndView handelShowEdit(HttpServletRequest req,HttpServletResponse res) throws Exception
	{
		int userid=0;
		Comments comment=new Comments();
		if (CommonServices.getLoggedUsername(req)!=null)
			userid=userdao.getByName(CommonServices.getLoggedUsername(req)).getId();
		int commentid=CommonServices.getIDFromRequest(req, "commentid");
		if (commentdao.get(commentid).getID()==userid)
		{
			comment=commentdao.get(commentid);
		}
		ModelMap model =new ModelMap();
		model.addAttribute("target","editcomment.htm");
		model.addAttribute("message","please enter infomation");
		model.addAttribute("comment",comment);
		return new ModelAndView("commentmodify","model",model);
	}
	
	@RequestMapping(value="/editcomment.htm",method=RequestMethod.POST)
	protected ModelAndView handelActEdit(HttpServletRequest req,HttpServletResponse res) throws Exception
	{
		Comments comment=getCommentFromRequest(req);
		ModelMap model=new ModelMap();
		if (commentdao.edit(comment, CommonServices.getIDFromRequest(req, "commentid")))
		{
			res.setIntHeader("sucess", 1);
			model.addAttribute("target","editcomment.htm");
			model.addAttribute("message","Add successed");
		}
		else
		{
			res.setIntHeader("sucess", 0);
			model.addAttribute("target","editcomment.htm");
			model.addAttribute("message","Add failed");
		}
		model.addAttribute("comment",comment);
		return new ModelAndView("commentmodify","model",model);
	}
	
	@RequestMapping(value="/delcomment.htm")
	protected ModelAndView handelActDel(HttpServletRequest req ,HttpServletResponse res) throws Exception
	{
		int id=CommonServices.getIDFromRequest(req, "commentid");
		if(commentdao.delete(id))
		{
			res.setIntHeader("sucess", 1);
		}
		else
		{
			res.setIntHeader("sucess", 0);
		}
		return null;
	}
	
	@RequestMapping(value="/approvecomment.htm")
	protected ModelAndView handelActApprove(HttpServletRequest req ,HttpServletResponse res) throws Exception
	{
		int id=CommonServices.getIDFromRequest(req, "commentid");
		if(commentdao.approve(id))
		{
			res.setIntHeader("sucess", 1);
		}
		else
		{
			res.setIntHeader("sucess", 0);
		}
		return null;
	}
	
	@RequestMapping(value="/disapprovecomment.htm")
	protected ModelAndView handelActDisapprove(HttpServletRequest req ,HttpServletResponse res) throws Exception
	{
		int id=CommonServices.getIDFromRequest(req, "commentid");
		if(commentdao.disapprove(id))
		{
			res.setIntHeader("sucess", 1);
		}
		else
		{
			res.setIntHeader("sucess", 0);
		}
		return null;
	}
	
	@RequestMapping(value="/allunapprovedcomment.htm")
	protected ModelAndView handelShowAllUnapproved (HttpServletRequest req,HttpServletResponse res)throws Exception
	{
		ModelMap model=new ModelMap();
		model.addAttribute("message",CommonServices.getMessageFromRequest(req));
		model.addAttribute("target","/allunapprovedcommentpage.htm");
		model.addAttribute("page",CommonServices.getPageFromRequest(req));
		return new ModelAndView("listajax","model",model);
	}
	
	@RequestMapping(value="/allunapprovedcommentpage.htm")
	protected ModelAndView handelShowAllUnapprovedContent (HttpServletRequest req,HttpServletResponse res)throws Exception
	{
		String filtername="getcommentunapproved";
		String sessionid=req.getSession().getId();
		if (CacheServices.getFromCache(cache, sessionid, filtername, 1)==null)
			CacheServices.insertToCache(cache, filtername, sessionid, commentdao.getAllUnapproved(), 10);
		int page=CommonServices.getPageFromRequest(req);
		List<Integer> commentids=CacheServices.getFromCache(cache, sessionid, filtername, page);
		List<Comments> comments=new ArrayList<Comments>();
		for(int i:commentids)
		{
			comments.add(commentdao.get(i));
		}
		int resultcount=CacheServices.getCountFromCache(cache, sessionid, filtername);
		int pagecount=CommonServices.getPageFromCount(resultcount);
		int pageresultcount=comments.size();
		ModelMap model=new ModelMap();
		model.addAttribute("comment",comments);
		model.addAttribute("pageresultcount",pageresultcount);
		res.addIntHeader("resultcount", resultcount);
		res.addIntHeader("pagecount", pagecount);
		res.addIntHeader("currentpage", page);
		return new ModelAndView("commentlist","model",model);
	}
	
	@RequestMapping(value="/listphotocomment.htm")
	protected ModelAndView handelShowPhotoList (HttpServletRequest req,HttpServletResponse res)throws Exception
	{
		int pid=CommonServices.getIDFromRequest(req, "photoid");
		ModelMap model=new ModelMap();
		model.addAttribute("message",CommonServices.getMessageFromRequest(req));
		model.addAttribute("target","/listphotocommentpage.htm?photoid="+pid+"");
		model.addAttribute("page",CommonServices.getPageFromRequest(req));
		return new ModelAndView("listajax","model",model);
	}
	
	@RequestMapping(value="/listphotocommentpage.htm")
	protected ModelAndView handelShowPhotoListContent (HttpServletRequest req, HttpServletResponse res) throws Exception
	{
		int pid =CommonServices.getIDFromRequest(req, "photoid");
		String filtername="getcomment?photoid="+pid+"";
		String sessionid=req.getSession().getId();
		if (CacheServices.getFromCache(cache, sessionid, filtername, 1)==null)
			CacheServices.insertToCache(cache, filtername, sessionid, commentdao.getByPhoto(pid), 10);
		int page=CommonServices.getPageFromRequest(req);
		List<Integer> commentids=CacheServices.getFromCache(cache, sessionid, filtername, page);
		List<Comments> comments=new ArrayList<Comments>();
		for(int i:commentids)
		{
			comments.add(commentdao.get(i));
		}
		int resultcount=CacheServices.getCountFromCache(cache, sessionid, filtername);
		int pagecount=CommonServices.getPageFromCount(resultcount);
		int pageresultcount=comments.size();
		ModelMap model=new ModelMap();
		model.addAttribute("comment",comments);
		model.addAttribute("pageresultcount",pageresultcount);
		res.addIntHeader("resultcount", resultcount);
		res.addIntHeader("pagecount", pagecount);
		res.addIntHeader("currentpage", page);
		return new ModelAndView("commentlist","model",model);
	}
	
	@RequestMapping(value="/listusercomment.htm")
	protected ModelAndView handelShowUserList (HttpServletRequest req,HttpServletResponse res)throws Exception
	{
		int uid=CommonServices.getIDFromRequest(req, "userid");
		ModelMap model=new ModelMap();
		model.addAttribute("message",CommonServices.getMessageFromRequest(req));
		model.addAttribute("target","/listphotocommentpage.htm?photoid="+uid+"");
		model.addAttribute("page",CommonServices.getPageFromRequest(req));
		return new ModelAndView("listajax","model",model);
	}
	
	@RequestMapping(value="/listusercommentpage.htm")
	protected ModelAndView handelShowUserListContent (HttpServletRequest req, HttpServletResponse res) throws Exception
	{
		int uid =CommonServices.getIDFromRequest(req, "userid");
		String filtername="getcomment?userid="+uid+"";
		String sessionid=req.getSession().getId();
		if (CacheServices.getFromCache(cache, sessionid, filtername, 1)==null)
			CacheServices.insertToCache(cache, filtername, sessionid, commentdao.getByUser(uid), 10);
		int page=CommonServices.getPageFromRequest(req);
		List<Integer> commentids=CacheServices.getFromCache(cache, sessionid, filtername, page);
		List<Comments> comments=new ArrayList<Comments>();
		for(int i:commentids)
		{
			comments.add(commentdao.get(i));
		}
		int resultcount=CacheServices.getCountFromCache(cache, sessionid, filtername);
		int pagecount=CommonServices.getPageFromCount(resultcount);
		int pageresultcount=comments.size();
		ModelMap model=new ModelMap();
		model.addAttribute("comment",comments);
		model.addAttribute("pageresultcount",pageresultcount);
		res.addIntHeader("resultcount", resultcount);
		res.addIntHeader("pagecount", pagecount);
		res.addIntHeader("currentpage", page);
		return new ModelAndView("commentlist","model",model);
	}
	
	private Comments getCommentFromRequest(HttpServletRequest req)
	{
		if (req.getParameterMap().containsKey("comment"))
		{
			Comments comment=new Comments();
			comment.setApproved(true);
			comment.setContent(req.getParameter("content"));
			comment.setPhotoID(CommonServices.getIDFromRequest(req, "photoid"));
			comment.setTitle(req.getParameter("title"));
			comment.setUserID(userdao.getByName(CommonServices.getLoggedUsername(req)).getId());
			return comment;
		}
		else
		{
			return null;
		}
	}

}
